<template>
    <a-modal :title="title"
             :visible="visible"
             :width="1200"
             @cancel="$emit('update:visible',false)"
    >
        <template slot="footer">
            <a-button key="back" @click="close">取消</a-button>
            <a-button key="submit" type="primary" :loading="WRITESUBMITTEDloading" @click="handleOk('WRITESUBMITTED')">
                保存
            </a-button>
            <a-button key="other" type="primary" :loading="WRITEPASTloading" @click="handleOk('WRITEPAST')">
                提交
            </a-button>
        </template>
        <a-spin :spinning="loading">
            <a-form-model ref="form" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol" :rules="rules">
                <a-form-model-item label="销项说明" prop="salesDescription">
                    <BaseTextarea v-model.trim="queryParam.salesDescription" />
                </a-form-model-item>
                <a-form-model-item label="销项证明文件上传" prop="fileArrays">
                    <l-table v-if="visible" :uploadArrys.sync="queryParam.fileArrays"
                             :businessCode="businessCode"></l-table>
                </a-form-model-item>
            </a-form-model>
        </a-spin>
        <!-- 选择审批人 -->
      <selectApprover
        :business-id="businessId"
        :process-config="setProcessConfig"
        :is-close-upper="false"
      />
    </a-modal>
</template>

<script>

export default {
  name:'a'+Date.now(),
  props: {
        visible: {
            type: Boolean,
            default: () => false
        },
        title: {
            type: String
        },
        row: {
            type: Object,
            default: () => null
        },
        submitFunc: {
            type: Function,
            default: () => null
        }
    },
    data() {
        return {
            loading: false,
            WRITESUBMITTEDloading: false,
            WRITEPASTloading: false,
            queryParam: {
                salesDescription: '',
                fileArrays: []
            },
            labelCol: { span: 3 },
            wrapperCol: { span: 21 },
            businessCode: 'LCM01',
            rules: {
                salesDescription: [{ required: true, message: '请输入销项说明', trigger: 'blur' }],
                // fileArrays: [{ type: 'array', required: true, message: '请上传文件', trigger: 'change' }]
            },
             // 流程配置
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            // 业务id
            businessId: null
        }
    },
   async mounted(){
       //获取通过获取流程配置值
       this.processConfig = await this.$getConfigFn('legal_claim_demage')
    },
    watch: {
        visible(e) {
            if (e) {
                this.queryParam = this.$clone(this.row.output)
            } else {
                this.queryParam = {}
                this.$refs.form.clearValidate()
            }
        }
    },
    methods: {
        close() {
            this.$emit('update:visible', false)
        },
        handleOk(status) {
           this.setProcessConfig = null;    
            this.$refs.form.validate(async (e) => {
                if (e) {
                    this.loading = true
                    this[status+'loading'] = true
                    this.queryParam.auditStatusEnum = status
                    this.queryParam.id = this.row.id
                    this.queryParam.businessCode = 'LCM06'
                    this.submitApprovalConfirm(status == 'WRITEPAST'?this.processConfig:6, async (flag)=>{
                     if(!flag) {
                       this[status+'loading'] = false
                       return
                     }
                    let res = await this.submitFunc(this.queryParam)
                    const msg = {
                     'WRITESUBMITTED': '保存',
                     'WRITEPAST': '提交'
                    }
                    if ((res.code === 200) || (res.data.code === 200)) {
                        this.$message.success(msg[status] + '成功')
                        if(status == 'WRITEPAST'){
                          this.$emit('setApprover',res)
                            //提交时，返回id
                            this.businessId = res.data.id;
                            this.setProcessConfig = this.processConfig;
                          }
                        this.close()
                        this.$emit('ok')
                    } else {
                        this.$message.warning(msg[status] + '失败')
                    }
                    this.loading = false
                    this[status+'loading'] = false
                    })
                }else {
                  this.$message.warning("请填写完所有必填项信息")
                }
            })
        }

    }
}
</script>

<style scoped>
</style>
